import { createBrowserRouter, RouterProvider, Navigate, RouteObject } from "react-router-dom";
import Login from "@renderer/views/login";

// 动态导入所有路由模块
const metaRouters: Record<string, { [key: string]: RouteObject[] }> = import.meta.glob("./modules/*.tsx", {
	eager: true
});

// 处理路由
const routerArray: RouteObject[] = [];

Object.keys(metaRouters).forEach((item: string) => {
	const moduleExports = metaRouters[item];
	Object.keys(moduleExports).forEach((key: string) => {
		const routes = moduleExports[key];
		if (Array.isArray(routes)) {
			// 添加类型检查确保每个路由项都符合 RouteObject 类型
			routes.forEach(route => {
				routerArray.push(route);
			});
		}
	});
});

const routerList: RouteObject[] = [
	{
		path: "/",
		element: <Navigate to="/login" replace />
	},
	{
		path: "/login",
		element: <Login />
	},
	...routerArray
];

const router = createBrowserRouter(routerList);

const Router = (): React.ReactNode => {
	return <RouterProvider router={router} />;
};

export default Router;
